package com.dept;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.dept.dao.DeptMapper;
import com.dept.dao.StaffMapper;
import com.dept.entity.DeptEntity;
import com.dept.entity.StaffEntity;
import com.lei.myBatis.dao.RuleDaoMapper;
import com.lei.myBatis.entity.RuleEntity;

import junit.framework.Assert;

public class DeptTest {
	private static SqlSessionFactory sessionFactory;
	private static final String loaction="com/dept/config.xml";
	static{
		try {
			InputStream stream=Resources.getResourceAsStream(loaction);
			sessionFactory=new SqlSessionFactoryBuilder().build(stream);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	public static SqlSessionFactory getSession(){
		return sessionFactory;
	}
	@Test
	public void addDept(){
		SqlSession session=sessionFactory.openSession();
		DeptMapper deptMapper=session.getMapper(DeptMapper.class);
		DeptEntity dept=new DeptEntity();
		dept.setName("name");
		dept.setDeptId("deptId");
		try {
			deptMapper.addDept(dept);
			session.commit();
			System.out.println("id: "+dept.getId());
		   Assert.assertNotNull(dept.getId());
		} catch (Exception e) {
			System.out.println("提交事务发生异常: "+e.getMessage());
		} 
		StaffMapper staffMapper=session.getMapper(StaffMapper.class);
		StaffEntity staff=new StaffEntity();
		staff.setStaffName("staffName");
		staff.setDeptId(String.valueOf(dept.getId()));
		try {
			staffMapper.addStaff(staff);
			session.commit();
			System.out.println("id: "+staff.getId());
		} catch (Exception e) {
			System.out.println("提交事务发生异常: "+e.getMessage());
		} finally{
			session.close();
		}
	}
//	SqlSession session=sessionFactory.openSession();
//	RuleDaoMapper ruleDaoMapper=session.getMapper(RuleDaoMapper.class);
//	RuleEntity rule=new RuleEntity();
//	rule.setParentRuleNo("parentRuleNo");
//	rule.setRuleNo("ruleNo");
//	rule.setRuleName("ruleName");
//	try {
//		ruleDaoMapper.addRule(rule);
//		session.commit();
//		System.out.println("id: "+rule.getId());
//	} catch (Exception e) {
//		System.out.println("提交事务发生异常: "+e.getMessage());
//	} finally{
//		session.close();
//	}
}
